package com.example.qrcode.controller;

import com.example.qrcode.entity.CurrentData;
import com.example.qrcode.mapper.CurrentDataMapper;
import com.example.qrcode.mapper.ProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestDataSourceController {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CurrentDataMapper currentDataMapper;

    @GetMapping("/datasource")
    public Map<String, Object> testDataSource() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 测试qr-code数据源
            String deviceId = productMapper.getDeviceIdByProductSNo("test123");
            result.put("qrCodeTest", "Success - deviceId: " + deviceId);

            // 测试power-cloud数据源
            List<CurrentData> dataList = currentDataMapper.selectList(null);
            result.put("powerCloudTest", "Success - records: " + dataList.size());

        } catch (Exception e) {
            log.error("数据源测试失败", e);
            result.put("error", e.getMessage());
        }
        return result;
    }
}

